package com.order.service;

import com.order.entity.SettlementRecord;
import com.order.mapper.SettlementRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 结算服务
 */
@Service
public class SettlementService {

    @Autowired
    private SettlementRecordMapper settlementRecordMapper;

    /**
     * 获取结算记录列表
     */
    public List<SettlementRecord> getSettlementList(Long techId, Integer status) {
        return settlementRecordMapper.selectByTechId(techId, status);
    }

    /**
     * 创建结算记录
     */
    public void createSettlement(Long orderId, Long techId, java.math.BigDecimal amount) {
        SettlementRecord record = new SettlementRecord();
        record.setOrderId(orderId);
        record.setTechId(techId);
        record.setAmount(amount);
        record.setStatus(0); // 待结算
        record.setCreateTime(LocalDateTime.now());
        
        settlementRecordMapper.insert(record);
    }

    /**
     * 结算
     */
    @Transactional
    public void settleOrder(Long settlementId) {
        SettlementRecord record = settlementRecordMapper.selectById(settlementId);
        if (record != null && record.getStatus() == 0) {
            record.setStatus(1); // 已结算
            record.setSettleTime(LocalDateTime.now());
            settlementRecordMapper.updateById(record);
        }
    }
}